<template>
  <div class="rule-users">
    <el-dialog title="分配角色" :visible.sync="ruleVis" @close="cancel">
      <p>当前用户:{{roleObj.username}}</p>
      <p>当前角色:{{roleObj.role_name}}</p>
      <el-form :model="ruleForm" ref="roleForm">
        <el-form-item label="分配角色" label-width="100px">
          <el-select v-model="ruleForm.role" placeholder="请选择角色">
            <el-option v-for="item in roleList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRoleList,setUserRole} from "../../api"
export default {
  name: "RoleUser",
  props:{
    ruleVis:{
      type:Boolean,
      default:true
    },
    roleObj:Object
  },
  created() {
    getRoleList().then(res => {
      console.log(res)
      this.roleList = res.data
    })
  },
  data() {
    return {
      ruleForm:{
        role:""
      },
      roleList:[]
    }
  },
  methods:{
    cancel(){
      this.ruleForm.role = ""
      this.$emit("cancel")
    },
    comfirm(){
      setUserRole({
        id:this.roleObj.id,
        rid:this.ruleForm.role
      }).then(res => {
        console.log(res)
        this.$emit("confirm")
      })
    }
  }
};
</script>

<style scoped>
  p{
    line-height: 40px;
  }
  .el-form{
    margin-top: 20px;
  }
</style>